package com.sykj.springboot.service.impl;

import com.sykj.springboot.entity.SysMenu;
import com.sykj.springboot.dao.SysMenuMapper;
import com.sykj.springboot.entity.vo.SysMenuVo;
import com.sykj.springboot.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author derking
 * @since 2022-09-20
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public List<SysMenuVo> getAllMenuList() {
        List<SysMenuVo> listone = new ArrayList<>();
        List<SysMenu> list = list();

        if (list != null){
            List<SysMenuVo> listbak = new ArrayList<>();
            for (SysMenu menu:list) {
                SysMenuVo sysMenuVo = new SysMenuVo();
                BeanUtils.copyProperties(menu,sysMenuVo);
                listbak.add(sysMenuVo);
            }




            //分离一级
            listone = listbak.stream().filter(u -> u.getLevel().equals("1")).collect(Collectors.toList());
            //分离二级
            List<SysMenuVo> listtwo = listbak.stream().filter(u -> u.getLevel().equals("2")).collect(Collectors.toList());
            //分离三级
            List<SysMenuVo> listthree = listbak.stream().filter(u -> u.getLevel().equals("3")).collect(Collectors.toList());

            for (SysMenuVo menu2:listtwo) {
                String menu2id = menu2.getId();
                List<SysMenuVo> childList = menu2.getChildList();
                for (SysMenuVo menu3:listthree) {
                    if (menu3.getParentId().equals(menu2id)){
                        if (childList == null){
                            childList = new ArrayList<>();
                            childList.add(menu3);
                            menu2.setChildList(childList);
                        }else{
                            childList.add(menu3);
                        }
                    }
                }
            }

            for (SysMenuVo menu1:listone) {
                String menu1id = menu1.getId();
                List<SysMenuVo> childList = menu1.getChildList();
                for (SysMenuVo menu2:listtwo) {
                    if (menu2.getParentId().equals(menu1id)){
                        if (childList==null){
                            childList = new ArrayList<>();
                            childList.add(menu2);
                            menu1.setChildList(childList);
                        }else{
                            childList.add(menu2);
                        }
                    }
                }
            }
        }
        return listone;
    }
}
